//
//  PTStuLSPartJobDetailContentCell.m
//  PlanTech
//
//  Created by ruifeng song on 2018/7/12.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTStuLSPartJobDetailContentCell.h"
#import "XHStarRateView.h"
#import "SINACalendarUtils.h"

@interface PTStuLSPartJobDetailContentCell()

@property(nonatomic,strong)UILabel * nameLabel;
@property(nonatomic,strong)UIImageView * headerImageView;
@property(nonatomic,strong)UILabel * serviceTypeLabel;
@property(nonatomic,strong)UILabel * userAddressLabel;
@property(nonatomic,strong)UILabel * addressTitlelabel;
@property(nonatomic,strong)UILabel * timeLabel;
@property(nonatomic,strong)UILabel * phoneLabel;
@property(nonatomic,strong)UILabel * remarkNameLabel;
@property(nonatomic,strong)UILabel * remarkLabel;
@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)UILabel * statusLabel;
@property(nonatomic,strong)UIView * headerView;
@property(nonatomic,strong)XHStarRateView *starRateView;

@property(nonatomic,copy)NSString *phoneNum;

@end

@implementation PTStuLSPartJobDetailContentCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        _backView = [[UIView alloc]initWithFrame:CGRectMake(12, 10, ScreenWidth -24, 195)];
        _backView.layer.cornerRadius = 6;
        _backView.layer.masksToBounds = YES;
        _backView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_backView];
        
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,_backView.width, 70)];
        [_backView addSubview:_headerView];
        
        _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, (_headerView.height - 46)/2, 46, 46)];
        _headerImageView.backgroundColor = [UIColor redColor];
        _headerImageView.layer.cornerRadius = 23;
        _headerImageView.layer.masksToBounds = YES;
        [_headerView addSubview:_headerImageView];
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_headerImageView.right + 7,25, 100, 12)];
//        _nameLabel.text = @"张哈哈";
        _nameLabel.font = PTFONT_12;
        _nameLabel.textColor = PTColor_2e2e2e;
        [_headerView addSubview:_nameLabel];
        
        UIButton * callPhoneButton= [UIButton buttonWithType:UIButtonTypeCustom];
        [callPhoneButton setFrame:CGRectMake(_headerView.width - 20 - 50- 9-20, (_headerView.height - 30)/2, 30, 30)];
        [callPhoneButton setImage:[UIImage imageNamed:@"call_phone_img"] forState:UIControlStateNormal];
        [callPhoneButton addTarget:self action:@selector(onClickCallPhoneButton:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:callPhoneButton];
        
        _statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(callPhoneButton.right + 7,(_headerView.height - 15)/2, 50, 15)];
        _statusLabel.text = @"待完成";
        _statusLabel.font = PTFONT_15;
        _statusLabel.textColor = PTColor_349ef7;
        [_headerView addSubview:_statusLabel];
        
        _starRateView = [[XHStarRateView alloc]initWithFrame:CGRectMake(_headerImageView.right + 7, _nameLabel.bottom + 7, 58, 10) numberOfStars:5 selectNumberStars:0 rateStyle:WholeStar isAnination:YES isTouch:NO finish:^(CGFloat currentScore) {
            
        }];
        [_headerView addSubview:_starRateView];
        
        UILabel * lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,70, _backView.width - 20, 0.5)];
        lineLabel.backgroundColor = PTColor_e0e0e0;
        [_backView addSubview:lineLabel];
        
        _serviceTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,lineLabel.bottom + 13, lineLabel.width-20, 12)];
//        _serviceTypeLabel.text = @"服务类型：超市代购";
        _serviceTypeLabel.font = PTFONT_12;
        _serviceTypeLabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_serviceTypeLabel];
        
        _addressTitlelabel = [[UILabel alloc]initWithFrame:CGRectMake(20,_serviceTypeLabel.bottom + 13, 62, 12)];
        _addressTitlelabel.text = @"收货地址：";
        _addressTitlelabel.font = PTFONT_12;
        _addressTitlelabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_addressTitlelabel ];
        
        _userAddressLabel = [[UILabel alloc]initWithFrame:CGRectMake(_addressTitlelabel.right,_serviceTypeLabel.bottom  +12, _serviceTypeLabel.width - 62, 12)];
//        _userAddressLabel.text = @"某年某日某天地址地址地址地址地址地址地址";
        _userAddressLabel.font = PTFONT_12;
        _userAddressLabel.numberOfLines = 5;
        _userAddressLabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_userAddressLabel ];
        
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,_userAddressLabel.bottom +11, _serviceTypeLabel.width, 12)];
//        _timeLabel.text = @"收货时间：10:00-12:00";
        _timeLabel.font = PTFONT_12;
        _timeLabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_timeLabel ];
        
        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,_timeLabel.bottom + 13, _timeLabel.width, 12)];
//        _phoneLabel.text = @"联系电话：18888888888";
        _phoneLabel.font = PTFONT_12;
        _phoneLabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_phoneLabel ];
        
        _remarkNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,_phoneLabel.bottom + 13, 40, 12)];
        _remarkNameLabel.text = @"备注：";
        _remarkNameLabel.font = PTFONT_12;
        _remarkNameLabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_remarkNameLabel];
        
        _remarkLabel = [[UILabel alloc]initWithFrame:CGRectMake(_remarkNameLabel.right,_phoneLabel.bottom + 12, _phoneLabel.width - 40, 12)];
//        _remarkLabel.text = @"什么事没什么什么事没什么什么什么什么什么什么什打大萨达所大所大萨达撒大所大萨达撒大所大声道撒";
        _remarkLabel.font = PTFONT_12;
        _remarkLabel.numberOfLines = 5;
        _remarkLabel.textColor = PTColor_2e2e2e;
        [_backView addSubview:_remarkLabel];
        
    }
    return self;
}


-(void)updatePartJobDetailContentCellWith:(LifeServiceMissionDetailModel *)missionDetailModel
{
    if (missionDetailModel ==nil) {
        return;
    }
    self.nameLabel.text = missionDetailModel.employer.name;
    
    if (missionDetailModel.employer.avatar && missionDetailModel.employer.avatar.length > 0) {
        
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:missionDetailModel.employer.avatar] placeholderImage:[UIImage imageNamed:@"list_header_default_img"]];
    }
    else
    {
        [self.headerImageView setImage:[UIImage imageNamed:@"list_header_default_img"]];
    }
    
    self.phoneNum = missionDetailModel.employer.phone;
    
    CGFloat starNum = 0;
    
    if (missionDetailModel.employer.avgRating !=nil) {
        
        starNum = [missionDetailModel.employer.avgRating floatValue];
    }
    [self.starRateView setCurrentScore:starNum];
    
    if ([missionDetailModel.status integerValue] == PTLifeServiceMission_NormalStatus) {
        
        self.statusLabel.text = @"待完成";
    }
    else if ([missionDetailModel.status integerValue] == PTLifeServiceMission_CommentStatus)
    {
        self.statusLabel.text = @"已评价";
    }
    else if ([missionDetailModel.status integerValue]== PTLifeServiceMission_WaitSolveStatus)
    {
        self.statusLabel.text = @"待处理";
    }
    else if ([missionDetailModel.status integerValue] == PTLifeServiceMission_FinishStatus)
    {
        self.statusLabel.text = @"待评价";
    }
    
    NSString * serviceType = @"";
    
    switch ([missionDetailModel.type integerValue]) {
        case PTLifeService_ExpressType:
            serviceType = @"代取快递";
            break;
        case PTLifeService_PrintType:
            serviceType = @"打印复印";
            break;
        case PTLifeService_BuyMealType:
            serviceType = @"捎饭带饭";
            break;
        case PTLifeService_HotboomType:
            serviceType = @"超市代购";
            break;
    }
    self.serviceTypeLabel.text= [NSString stringWithFormat:@"服务类型：%@",serviceType];
    
    self.userAddressLabel.text= missionDetailModel.employerAddress;
    
    //收货地址动态高度：
    CGFloat addressHeight = [self.userAddressLabel.text heightForFont:self.userAddressLabel.font width:_userAddressLabel.width];
    [self.userAddressLabel setFrame:CGRectMake(self.addressTitlelabel.right,self.serviceTypeLabel.bottom + 12, self.serviceTypeLabel.width - 62, addressHeight)];
    
    NSString *startimeStr=[NSString stringWithFormat:@"%@",missionDetailModel.startTime];
    NSString *endTimeStr=[NSString stringWithFormat:@"%@",missionDetailModel.endTime];
    
    self.timeLabel.text = [NSString stringWithFormat:@"收货时间：%@-%@",[SINACalendarUtils getDateStringWithTimeStr:startimeStr andFormat:@"MM月dd日 HH:mm"],[SINACalendarUtils getDateStringWithTimeStr:endTimeStr andFormat:@"HH:mm"]];
    
    [self.timeLabel setFrame:CGRectMake(20, self.userAddressLabel.bottom + 11, self.serviceTypeLabel.width, 12)];
   
    self.phoneLabel.text = [NSString stringWithFormat:@"联系电话：%@",missionDetailModel.employer.phone];

    self.remarkLabel.text = missionDetailModel.remark;
    
    //备注内容动态高度：
    CGFloat remarkHeight = [self.remarkLabel.text heightForFont:self.remarkLabel.font width:self.remarkLabel.width];
    if (remarkHeight == 0) {
        
        remarkHeight= 15;
    }
    [self.remarkLabel setFrame:CGRectMake(self.remarkNameLabel.right,self.phoneLabel.bottom  +12, self.phoneLabel.width - 40,remarkHeight)];
    
    [self.backView setFrame:CGRectMake(12, 10, ScreenWidth - 24, remarkHeight + addressHeight + 33 + 75 + 70)];
}

-(void)onClickCallPhoneButton:(id)sender
{
    NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",self.phoneNum];
    
    WISAlertView * alertView = [[WISAlertView alloc]initWithTitle:@"拨打电话" message:string sureBtn:@"确定" cancleBtn:@"取消"];
   
    alertView.resultIndex= ^(NSInteger index) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    };
     [alertView showXLAlertView];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
